import { _decorator, Component, Label, Node } from 'cc';
import { AudioManager } from '../../../common/src/manager/AudioManager';
import { UIManager } from '../../../framework/ui/uimanager/UIManager';
import { GGStore } from '../../../framework/core/base/GGStore';
import { MonoUIPanel } from '../../../common/src/mono/MonoUIPanel';
import DataMgr from '../../../common/src/util/DataMgr';
import { EVENT_NAMES, GAME_DATA_ENUMS, TIP_ENUMS } from '../../../common/src/util/Enums';
import { WECHAT } from 'cc/env';
import SDKManager from '../../../common/src/manager/SDKManager';
const { ccclass, property } = _decorator;

@ccclass('InviteDialog')
export class InviteDialog extends MonoUIPanel {
    // 是否开启的 tag
    private unlockTag: boolean[] = []
    private unlockArr: number[] = [1, 3, 5]
    
    start(): void {
        this.initValues()
        this.initViews()
    }

    // ---------------------------- 初始化 ---------------------------- //
    private initValues() {
        for (var i = 0; i < 3; i++) {
            const value = GGStore.get(GAME_DATA_ENUMS.UNLOCK_INVITE + i, false)
            this.unlockTag[i] = value
        }
    }

    private initViews () {
        for (var i = 0; i < 3; i++) {
            const giftNode = this.node.getChildByName("bg").getChildByName("Gift" + i)
            if (giftNode) {

                if (this.unlockTag[i] == true) {
                    giftNode.getChildByName("Count").getComponent(Label).string = "已邀请"
                    giftNode.getChildByName("yilingqu").active = true
                    giftNode.getChildByName("SelBg").active = false
                } else {
                    if (DataMgr.inviteCount >= this.unlockArr[i]) {
                        giftNode.getChildByName("Count").getComponent(Label).string = "可领取"
                        giftNode.getChildByName("SelBg").active = true
                    } else {
                        giftNode.getChildByName("Count").getComponent(Label).string = "邀请" + DataMgr.inviteCount + "/" + this.unlockArr[i]
                        giftNode.getChildByName("SelBg").active = false
                    }
                    
                    giftNode.getChildByName("yilingqu").active = false
                }
            }
        }
    }

    // ---------------------------- 事件 ---------------------------- //
    public onShareClicked(){
        AudioManager.playButton();

        const self = this
        if (WECHAT) {
            SDKManager.showRewardVideoAd("邀请分享标记点位", (err, res) => {
                if (!err) {
                    UIManager.open("tip:res/prefab/TipPanel", (err, res:Node) => {
                        res.getComponent("TipPanel").setData(TIP_ENUMS.SHARE_SUCCESS)
                    });
                    
                    DataMgr.inviteCount = DataMgr.inviteCount + 1
                    self.initViews()
                    self.onClosePanel()
                } else {
                    UIManager.open("tip:res/prefab/TipToast", (err, node:Node) => {
                        if(res.code == 1001){ 
                            node.getComponent("TipToast").setData(TIP_ENUMS.LOAD_VIDEO_FAIL)
                        }else{
                            node.getComponent("TipToast").setData(TIP_ENUMS.NOT_FULL_VIDEO)
                        }
                    });  
                }
            })
        } else {
            DataMgr.inviteCount = DataMgr.inviteCount + 1
            self.initViews()
            self.onClosePanel()
        }
    }

    /** 关闭面板. */
    private async onClosePanel() {
        AudioManager.playButton();
        await this.onExitAnimation();
        UIManager.close("invite:res/prefab/InviteDialog");
    }

    // 获取道具
    private onGetPropertyClicked(event, data) {
        if (this.unlockTag[data] == false) {
            if (DataMgr.inviteCount >= this.unlockArr[data]) {
                console.log ("可以解锁 " + DataMgr.inviteCount + " - " + this.unlockArr[data])
                this.unlockTag[data] = true
                GGStore.set(GAME_DATA_ENUMS.UNLOCK_INVITE + data, true)

                this.initViews()

                if (data == 0) {
                    DataMgr.money = DataMgr.money + 100
                    gg.systemEvent.emit(EVENT_NAMES.UPDATE_START_INFO)
                } else if (data == 1) {
                    DataMgr.magnetCount = DataMgr.magnetCount + 1
                } else if (data == 2) {
                    DataMgr.addCount = DataMgr.addCount + 1
                }
            }
        }
    }
}


